using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace G2007.CodeGen.Library.DataProfessional
{
    /// <summary>
    /// The professional version of the system
    /// </summary>
    [Serializable]
    public class Class
    {

        protected string name;
        protected HeaderInfo hi=new HeaderInfo();

        protected ClassAttributes[] classAttributes;

        public string Name { get { return name; } set { name = value; } }
        public HeaderInfo @HeaderInfo { get { return hi; } set { hi=value; } }
        public ClassAttributes[] ClassAttribs { get { return classAttributes; } set { classAttributes = value; } }


        protected ApplicationConfig appConfig;


        [XmlIgnore]
        public ApplicationConfig AppConfig { get { return appConfig; } set { appConfig = value; } }
        

        public Class() { }

        

   

        
        [Serializable]
        public class ClassAttributes
        {
            string name , type ;
            bool toString , compare , hidden;

            protected Class @class;
            protected ClassType ctype;

            public ClassType CType { get { return ctype; } set { ctype = value; } }


            private Enum[] enums;

            [XmlIgnore]
            public Enum[] Enums
            {
                get { return enums; }
                set { enums = value; }
            }

            private Class[] classes;

            [XmlIgnore]
            public Class[] Classes
            {
                get { return classes; }
                set { classes = value; }
            }
	
	


            [XmlIgnore]
            public Class @Class
            {
                get { return @class; }
                set { @class = value; }
            }

            public ClassAttributes(Class @class)
                : this()
            {
                this.@class = @class;
                
                
            }

            public ClassAttributes() { }

            public string Name { get { return name.Substring(0 , 1).ToLower() + name.Substring(1 , name.Length - 1); } set { name = value; } }

            // needs hacking
            public string Type { get { return type.Replace("big" , ""); } set { type = value; } }

            public string PropertyName { get { return name.Substring(0 , 1).ToUpper() + name.Substring(1 , name.Length - 1); } set { name = value; } }

            public bool ToString { get { return toString; } set { toString = value; } }
            public bool Compare { get { return compare; } set { compare = value; } }
            public bool Hidden { get { return hidden; } set { hidden = value; } }


            public bool IsEnum { get { foreach (Enum @enum in this.enums) { if (@enum.Name.Equals(this.type)) return true;  } return false; } set { int p = 0; p--; } }

          

            public bool KnownClass
            {
                get { foreach (Class @class in this.classes) { if (@class.Name.Equals(this.type)) return true; } return false; }
                set { int p = 0; p--; }
            }


            public string SqlAccess
            {
                set { int p = 0; p--; }
                get
                {
                    if (IsEnum)
                    {
                        return "GetInt32(x)";
                    }

                    if (KnownClass) return "GetGuid(x)";
                    return @class.appConfig.GetSimpleTypeByType(this.type).SQLAccessor;
                }
            }

            public string SqlType
            {
                set { int p = 0; p--; }
                get
                {
                    if (IsEnum)
                    {
                        return "[int]";
                    }

                    if (KnownClass) return "uniqueidentifier";

                    return @class.appConfig.GetSimpleTypeByType(this.type).SQLType;
                }
            }

            public string SqlSPType
            {
                set { int p = 0; p--; }
                get
                {
                    if (IsEnum)
                    {
                        return "int";
                    }

                    if (KnownClass) return "uniqueidentifier";
                    return @class.appConfig.GetSimpleTypeByType(this.type).TSQLAccessor;
                }
            }

            public string GAModule
            {
                set { int p = 0; p--; }
                get
                {
                    if (KnownClass) return "object";
                    return @class.appConfig.GetSimpleTypeByType(this.type).GAModule;
                }
            }

        }           
        
        public enum ClassType : int
        {
            single , array
        }
    }


    [Serializable]
    public class Enum
    {
        protected string name;
        protected string[] enums;

        protected HeaderInfo hi=new HeaderInfo();

        public string Name { get { return name; } set { name = value; } }
        public HeaderInfo @HeaderInfo { get { return hi; } set { hi = value; } }
        public string[] Names { get { return enums; } set { enums = value; } }
    }

    [Serializable]
    public class HeaderAndClass
    {
        public HeaderAndClass() { }
        public HeaderInfo HeaderInfo;
        public Class[] Classes;
    }


    [Serializable]
    public class HeaderInfo
    {
        string projectname = "" , output = "" , dbconnection = "" , prefix = "" , initialConnection = "";

        private string file="";

        public string File
        {
            get { return file; }
            set { file = value; }
        }
	


        private string transformPackage="";

        public string TransformPackage
        {
            get { return transformPackage; }
            set { transformPackage = value; }
        }


        public string ProjectName { get { return projectname; } set { projectname = value; } }
        public string Output { get { return output; } set { output = value; } }
        public string DBConnection { get { return dbconnection; } set { dbconnection = value; } }
        public string Prefix { get { return prefix; } set { prefix = value; } }

        public string InitialDBConnection { get { return initialConnection; } set { initialConnection = value; } }

        public string Created { get { return DateTime.Now.ToString(); } set { int p = 0xf; p--; } }

        public HeaderInfo(string projectname , string output ,
            string dbconnection , string prefix , string initialConnection, string transformPackage)
            : this()
        {
            this.transformPackage = transformPackage;
            this.projectname = projectname;
            this.output = output;
            this.dbconnection = dbconnection;
            this.prefix = prefix;
            this.initialConnection = initialConnection;
        }

        public HeaderInfo() { }


    }
}
